package xyz.jiang.auth.config;

import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import xyz.jiang.common.mapper.UsersMapper;
import xyz.jiang.common.pojo.entity.Users;

import java.util.List;

import static xyz.jiang.common.constants.SystemConstants.SYSTEM_CACHE;
import static xyz.jiang.common.constants.UserConstants.USER_CACHE;

@Component
public class UserCacheInitConfig {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @PostConstruct
    public void init() {
        List<Users> userList = usersMapper.selectList(null);
        if (CollectionUtils.isEmpty(userList)) {
            return;
        }
        for (Users user : userList) {
            String id = user.getId();
            redisTemplate.opsForValue().set(SYSTEM_CACHE + USER_CACHE + id,user);
        }
    }
}
